<?php

use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\grid\GridView;

/**
 * @var yii\web\View $this
 * @var amnah\yii2\user\Module $module
 * @var array $actions
 */
global $regionTypes2;
global $prodAttrCodes;
$regionTypes2 = $regionTypes;
$module = $this->context->module;

$this->title = Yii::t('user', 'Yii 2 User');
?>
<div class="user-default-index">




    <?php ?>
    <h3><?php echo $prodSpecName . '管理'; ?></h3>
    <div class="row">
        <div class="col-md-3">
            <div class="info-box">
                <!-- Apply any bg-* class to to the icon to color it -->
                <span class="info-box-icon bg-red"><i class="fa fa-star-o"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">全部楼盘</span>
                    <span class="info-box-number"><?php echo $dataProvider->getTotalCount(); ?></span>
                </div><!-- /.info-box-content -->
            </div><!-- /.info-box -->
        </div>
        <div class="col-md-3">
            <div class="info-box">
                <!-- Apply any bg-* class to to the icon to color it -->
                <span class="info-box-icon bg-red"><i class="fa fa-star-o"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">已租</span>
                    <span class="info-box-number">93,139</span>
                </div><!-- /.info-box-content -->
            </div><!-- /.info-box -->
        </div>
        <div class="col-md-3">
            <div class="info-box">
                <!-- Apply any bg-* class to to the icon to color it -->
                <span class="info-box-icon bg-red"><i class="fa fa-star-o"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">覆盖区域</span>
                    <span class="info-box-number"> <?php echo $regionCnt . '个'; ?>   </span>
                </div><!-- /.info-box-content -->
            </div><!-- /.info-box -->
        </div>
        <div class="col-md-3">
            <div class="info-box">
                <!-- Apply any bg-* class to to the icon to color it -->
                <span class="info-box-icon bg-red"><i class="fa fa-star-o"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">本日新增</span>
                    <span class="info-box-number"><?php echo $regionTodayCnt . '个'; ?> </span>
                </div><!-- /.info-box-content -->
            </div><!-- /.info-box -->
        </div>
    </div>
    <?php ?>
    <p>
        <?php
            $createUrl                     = [];
            $createUrl[]                   = 'create';
            $createUrl['prod_spec_id']   = $prod_spec_id;
            $createUrl['prod_spec_name'] = $prodSpecName;

            if ($parent_prod_id)
            {
                $createUrl['parent_prod_id'] = $parent_prod_id;
            }
            //创建区域逻辑
            // 如果创建
            echo Html::a( Html::tag('span', Yii::t('region', 'Create') .  $prodSpecName , ['class' => "glyphicon fa fa-plus-square"])   . $region_name. Yii::t('region', $region_type_name) , $createUrl, ['class' => 'btn btn-success']);

        ?>

    </p>

    <?php
        $prodAttrCodes=[];

       // Yii::trace("=====================              ============");
        //Yii::trace($dataProvider);


        // 增加展示的字段
        $columns = [];
        $i = 0;

        // 复选框
        $columns[$i]['class'] = 'yii\grid\CheckboxColumn';
        $columns[$i]['multiple'] = true;
        $columns[$i]['name'] = 'uid';
        $columns[$i]['options'] = ['style'=>'width:30px'];


        $i++;

        // id
        $columns[$i]['attribute'] = 'id';
        $columns[$i]['options'] = ['style'=>'width:30px'];

        $i++;
        //名称
        $columns[$i]['label'] = '名称';
        $columns[$i]['attribute'] = 'prod_name';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'prod_name', ['class'=>'form-control']);



        // 如果是楼盘需要取出子集
        if ($prod_spec_id==10001 || $prod_spec_id==10003 || $prod_spec_id==10004)
        {

            $subProdName = '收录房源';
            switch($prod_spec_id)
            {
                case 10001:
                    $subProdName = '收录房源';
                    break;

                case 10003:
                    $subProdName = '收录楼盘';
                    break;

                case 10004:
                    $subProdName = '收录大楼';
                    break;
                default:
                    $subProdName = '收录房源';
                    break;

            }

            $i++; // 区域
            $columns[$i]['label']  = $subProdName;
            $columns[$i]['format'] = 'raw';

            $columns[$i]['options'] = ['style'=>'width:90px'];
            $columns[$i]['value']   = function($model)
            {
                return Html::a(count($model->subProdInsts), ['classify', 'prod_spec_id'=>10002,   'parent_id'=> $model->id ], ['title' => "收录房源"]);

            };

        }
        else if ($prod_spec_id==10002)
        {
            $i++; // 区域
            $columns[$i]['label'] = '归属楼盘';
            $columns[$i]['format'] = 'raw';
          //  $columns[$i]['attribute'] = 'parentProdInsts.prod_name';
            $columns[$i]['options'] = ['style'=>'width:180px'];
            $columns[$i]['value']   = function($model) use($prodSpecName)
            {
                if (!$model->parentProdInsts)
                {
                    return Html::a('设置楼盘', ['update', 'prod_spec_id'=>10002,   'id'=> $model->id ,    'prod_spec_name' => $prodSpecName], ['title' => "归属楼盘"]);

                }
                return Html::a($model->parentProdInsts->prod_name, ['view', 'id'=>$model->parentProdInsts->id,  'prod_spec_id'=>10001,  'prod_spec_name' =>'楼盘'  ], ['title' => "归属楼盘"]);

            };
        }

//        $i++; // 区域
//        $columns[$i]['label'] = '二级区域名称'. $regionTypes[3];
//        //$columns[$i]['attribute'] = 'parentRegions.region_name';
//        $columns[$i]['options'] = ['style'=>'width:180px'];
//        $columns[$i]['content']   = function($model)
//        {
//            global $regionTypes2;
//            return $model->parentRegions->region_name  ;
//        };



        $i++; // 区域
        $columns[$i]['label'] = '区域名称';
        //$columns[$i]['attribute'] = 'region_id';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['content']   = function($model)
        {
            //global $regionTypes2;
            return $model->parentRegions->region_name . ' ' . $model->regions->region_name; //. $regionTypes2 [ $model->regions->region_type_id ]  ;
        };
        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'region_name', ['class'=>'form-control']);
       // $columns[$i]['filter'] = ['region_name'=>['1'=>'晋安','2'=>'鼓楼']];

        foreach ($attrSpecs as $attrSpec)
        {
            $prodAttr = $attrSpec->attrSpecs;
//            foreach ($attrSpec->attrSpecs as $key => $prodAttr )
//            {
                $i++;


               // $columns[$i]['attrcode'] = $prodAttr->attr_spec_id;
                $columns[$i]['label'] = $prodAttr->attr_spec_name;
                $prodAttrCodes[$i] = $prodAttr->attr_spec_id;
                $a = $prodAttr->attr_spec_id;
                //$columns[$i]['attribute']  = $prodAttr->attr_spec_id;
                $columns[$i]['value']   = function($model, $key, $index, $column)use ($a)
                {


                    foreach ($model->prodAttrInsts  as $prodAttrInst)
                    {
                        global $prodAttrCodes;
          //              Yii::trace($prodAttrCodes);
                      // Yii::trace($column);
                //        Yii::trace("=====================              ============");
//                        Yii::trace($index);
//                        Yii::trace($key);
//                        Yii::trace($prodAttrInst->attr_spec_id);
//                        Yii::trace($prodAttrInst->prod_id);
//                        Yii::trace($prodAttrInst->content);
//                        Yii::trace($prodAttrCodes[$index] );

                        if ($a == $prodAttrInst->attr_spec_id)
                        {
                            return $prodAttrInst->content;
                        }
                    }
                    return '未设置具体的值！';
                };

//                $columns[$i]['content']   = function($model)
//                {
//                    return $model->prodAttrInsts;
//                };
                $columns[$i]['options'] = ['style'=>'width:180px'];
               // echo $form->field($model, "attrSpec[{$prodAttrID->attr_spec_id}]")->label($prodAttrID->attr_spec_name)->textInput(['maxlength' => 64]);
//            }
        }


        $i++;
        //名称
        $columns[$i]['label'] = '产权客户';
        $columns[$i]['attribute'] = 'own_cust_id';
        $columns[$i]['options'] = ['style'=>'width:120px'];
        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'ownCustName', ['class'=>'form-control']);
        $columns[$i]['content']   = function($model)
        {
            if ($model->custs->cust_name)
            {
                return $model->custs->cust_name  ;
            }
            return '未指定'  ;
        };



        $i++;
        //名称
        $columns[$i]['label'] = '创建人';
        $columns[$i]['attribute'] = 'create_staff_id';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'user_name', ['class'=>'form-control']);
        $columns[$i]['content']   = function($model)
        {
            return $model->users->username  ;
        };


        $i++;
        //名称
        $columns[$i]['label'] = '更新时间';
        $columns[$i]['attribute'] = 'modify_date';
        $columns[$i]['options'] = ['style'=>'width:180px'];

        $i++;
        //操作选项
        $columns[$i]['class']   = 'app\common\grid\CustActionColumn';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['buttons'] =  [
            'view' => function ($url, $model, $key) use ($prodSpecName) {
                return Html::a(Html::tag('span', '查看', ['class' => "glyphicon fa fa-eye"]), ['view', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id,    'prod_spec_name' => $prodSpecName], ['class' => "btn btn-xs btn-success",'label'=>'查看子产品', 'title' => '查看子产品']);
            },

            'update' => function ($url, $model, $key) use ($prodSpecName) {
                return Html::a(Html::tag('span', '编辑', ['class' => "glyphicon fa fa-edit"]), ['update', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id,    'prod_spec_name' => $prodSpecName ], ['class' => "btn btn-xs btn-success",'label'=>'查看子产品', 'title' => '查看子产品']);
            },

            'delete' => function ($url, $model, $key)  {
                return Html::a(Html::tag('span', '删除', ['class' => "glyphicon fa fa-trash"]), ['delete', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id ], ['class' => "btn btn-xs btn-danger",'label'=>'删除产品', 'title' => '删除子产品']);
            },


            'create' => function ($url, $model, $key) use ($prodSpecName) {
                return Html::a(Html::tag('span', '订单生成', ['class' => "glyphicon fa fa-edit"]), ['/prod/order/create', 'prod_id'=>$model->id,       'prod_spec_name' => $prodSpecName ], ['class' => "btn btn-xs btn-success",'label'=>'生成一张订单', 'title' => '查看子产品']);
            },

        ];

//        $i++;
//        //测试
//        $columns[$i]['label'] = '测试';
//        $columns[$i]['content']   = function($model){ return "我的测试"; };
//        $columns[$i]['options'] = ['style'=>'width:180px'];

        echo GridView::widget([
        'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
        'layout' => "{items}{summary}{pager}",
        'options'=>['id'=>'grid'],
        'columns' => $columns
           // [

                // ...
//                [
//                    'class' => 'yii\grid\CheckboxColumn',//复选框
//                    'multiple' => true,
//                    'name' => 'uid',
//                    'filterOptions'=>['style'=>'width:30px']
//                ],
//
//                [
//                    'attribute' => 'id',
//                    'filterOptions'=>['style'=>'width:30px']
//                ],
//
//                [
//                    'label'=>'名称',
//                    'attribute'=>'prod_name',
//                    'options'=>['style'=>'width:180px']
//                ],
//                [
//                    'class' => 'app\common\grid\ActionColumn',
//                    'options'=>['style'=>'width:180px'],
//                    'buttons' => [
//                        'view' => function ($url, $model, $key) {
//                            return Html::a(Html::tag('span', '查看', ['class' => "glyphicon fa fa-eye"]), ['view', 'id'=>$model->id], ['class' => "btn btn-xs btn-success",'label'=>'查看子产品', 'title' => '查看子产品']);
//                        },
//                    ]
//                ],
          //  ],
        ]);


    ?>


</div>
